import { Consts } from "../consts/Consts";
import Tool from "./Tool";

/**
 * 日志级别定义
 */
export enum LOG_LEVEL {
	LOG = 'log',
	INFO = 'info',
	DEBUG = 'debug',
	WARN = 'warn',
	ERROR = 'error'
}

/**
 * 日志工具
 */
export default class LogUtil {
	private static _isCacheLog:boolean = false;
	public static set isCacheLog(value:boolean){
		this.isCacheLog = value;
	}
	public static get isCacheLog(){
		return this._isCacheLog;
	}
	private static logArr:Array<string> = [];
	/**
	 * 获取平台类型
	 */
	public static getPlatForm(){
		return uni.getSystemInfoSync().platform;
	}
	/**
	 * @param tag 标签
	 * @param info 日志内容
	 */
	public static log(level:LOG_LEVEL,tag:string,info:string | number){
		let levelTag :string | null = null; 
		switch (level){
			case LOG_LEVEL.LOG:
				levelTag = LOG_LEVEL.LOG;
				break;
			case LOG_LEVEL.DEBUG:
				levelTag = LOG_LEVEL.DEBUG;
				break;
			case LOG_LEVEL.INFO:
				levelTag = LOG_LEVEL.INFO;
				break;
			case LOG_LEVEL.WARN:
				levelTag = LOG_LEVEL.WARN;
				break;
			case LOG_LEVEL.ERROR:
				levelTag = LOG_LEVEL.ERROR;
				break;
			default:
				break;
		}
		if(levelTag == null){
			return;
		}
	    let	logInfo = levelTag + '[' + tag + ']' + info;
		console.log(logInfo);
	}
	/**
	 * @param isSave 清理前是否要保存在本地
	 */
	public static clearLog(isSave:boolean){
		if(isSave){
			this.saveToLocal();
		}else{
			Tool.removeItem(Consts.LOG_SAVE_KEY);
		}
	}
	/**
	 * 是否存在日志
	 */
	public static isHaveLocalLog():boolean{
		return Tool.getBool(Consts.LOG_STATUS_KEY);
	}
	
	 //-- 保存日志到本地
	private static saveToLocal() {
		if (this.logArr.length == 0) {
			Tool.removeItem(Consts.LOG_SAVE_KEY);
			return;
		}
		switch (uni.getSystemInfoSync().platform) {
			case 'mp-weixin':
				//微信小程序特例
				let mgr =  uni.getFileSystemManager()
				// mgr.getFileInfo({
				// 	filePath:
				// })
				break;
			default:
				uni.saveFile({
					tempFilePath:'logFile',
					
				})
				break;
		}
	}
}